using System;
using System.Collections.Generic;
using System.Text;
using SP.Parameter;

namespace SP.HwDeferredRS
{
    /// <summary>
    /// A fusion which flips the vertex-tangent according to the vertex color
    /// </summary>
    public class HardwareInstancingFusion : AbstractFusion
    {
        public HardwareInstancingFusion()
        {
        }

        public static string NAME = "HardwareInstancing";
        public override string Name
        {
            get { return NAME; }
        }

        public override string Desc
        {
            get { return "Enable this to support Hardware-Instancing"; }
        }

        public override Stage Stage
        {
            get { return Stage.Vertex1; }
        }

        public override int UVChannelCount
        {
            get { return 0; }
        }

        public override void Code(StringBuilder b, string platform)
        {
            AddLine(b, "vec4 pos;");
            AddLine(b, "vec3 t;");
            //AddLine(b, "vec3 b;");
            AddLine(b, "vec3 n;");

            AddLine(b, "pos.x = dot(input.Reserved0, pipeline.Pos_Ms);");
            AddLine(b, "pos.y = dot(input.Reserved1, pipeline.Pos_Ms);");
            AddLine(b, "pos.z = dot(input.Reserved2, pipeline.Pos_Ms);");
            AddLine(b, "pos.w = pipeline.Pos_Ms.w;");

            AddLine(b, "t.x = dot(input.Reserved0.xyz, pipeline.T_Ms);");
            AddLine(b, "t.y = dot(input.Reserved1.xyz, pipeline.T_Ms);");
            AddLine(b, "t.z = dot(input.Reserved2.xyz, pipeline.T_Ms);");

            //AddLine(b, "b.x = dot(input.Reserved0.xyz, pipeline.B_Ms);");
            //AddLine(b, "b.y = dot(input.Reserved1.xyz, pipeline.B_Ms);");
            //AddLine(b, "b.z = dot(input.Reserved2.xyz, pipeline.B_Ms);");

            AddLine(b, "n.x = dot(input.Reserved0.xyz, pipeline.N_Ms);");
            AddLine(b, "n.y = dot(input.Reserved1.xyz, pipeline.N_Ms);");
            AddLine(b, "n.z = dot(input.Reserved2.xyz, pipeline.N_Ms);");

            AddLine(b, "pipeline.Pos_Ms = pos;");
            AddLine(b, "pipeline.T_Ms = t;");
            //AddLine(b, "pipeline.B_Ms = b;");
            AddLine(b, "pipeline.N_Ms = n;");
        }
    }
}
